package com.campus.controller;

import com.campus.model.Class;
import com.campus.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@RestController
@RequestMapping("/classes")
public class ClassController {

    private final ClassService classService;

    @Autowired
    public ClassController(ClassService classService) {
        this.classService = classService;
    }

    @PostMapping("/create")
    public ResponseEntity<String> createClass(@RequestBody Class cls) {
        int result = classService.createClass(cls);
        return result > 0 ? ResponseEntity.ok("Class created successfully") : ResponseEntity.status(500).body("Failed to create class");
    }

    @PutMapping("/{id}/update")
    public ResponseEntity<String> updateClass(@PathVariable int id, @RequestBody Class cls) {
        cls.setId(id);
        int result = classService.updateClass(cls);
        return result > 0 ? ResponseEntity.ok("Class updated successfully") : ResponseEntity.status(500).body("Failed to update class");
    }

    @PostMapping("/{classId}/add-students")
    public ResponseEntity<String> addStudentsToClass(@PathVariable int classId, @RequestBody List<Integer> studentIds) {
        int result = classService.addStudentsToClass(classId, studentIds);
        return result > 0 ? ResponseEntity.ok("Students added successfully") : ResponseEntity.status(500).body("Failed to add students");
    }

    @PostMapping("/{classId}/assign-teachers")
    public ResponseEntity<String> assignTeachersToClass(@PathVariable int classId, @RequestBody List<Integer> teacherIds) {
        int result = classService.assignTeachersToClass(classId, teacherIds);
        return result > 0 ? ResponseEntity.ok("Teachers assigned successfully") : ResponseEntity.status(500).body("Failed to assign teachers");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Class> getClassById(@PathVariable int id) {
        Class cls = classService.getClassById(id);
        return cls != null ? ResponseEntity.ok(cls) : ResponseEntity.notFound().build();
    }

    @GetMapping("/activity/{activityId}")
    public ResponseEntity<List<Class>> getClassesByActivityId(@PathVariable int activityId) {
        List<Class> classes = classService.getClassesByActivityId(activityId);
        return ResponseEntity.ok(classes);
    }

    @GetMapping("/all")
    public ResponseEntity<List<Class>> getAllClasses() {
        List<Class> classes = classService.getAllClasses();
        return ResponseEntity.ok(classes);
    }

    @DeleteMapping("/{id}/delete")
    public ResponseEntity<String> deleteClass(@PathVariable int id) {
        int result = classService.deleteClass(id);
        return result > 0 ? ResponseEntity.ok("Class deleted successfully") : ResponseEntity.status(500).body("Failed to delete class");
    }
}
